﻿using System;

using System.Data;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class DaySaleIncomeIndex : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("IC1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }

                if (Session["FLT_DATE"] != null)
                    txtFltDate.Text = Convert.ToDateTime(Session["FLT_DATE"].ToString()).ToShortDateString();

                if (Request.QueryString["flt_no"] != null)
                {
                    txtFltDate.Text = Request.QueryString["flt_date"].ToString();
                }
                try
                {
                    lstCurrencies.SelectedValue = Session["DEFAULT_CURRENCY"].ToString();
                }
                catch (Exception ex)
                {
                    //Does nothing!
                }
            }
            lblError.Text = "";
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime.Parse(txtFltDate.Text);
            }
            catch
            {
                lblError.Text = "Invalid date format!";
                return;
            }

            Session["FLT_DATE"] = txtFltDate.Text;

            int ret;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "bll_airline_get_date_flights_sale_income_index";
            SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@flt_date", txtFltDate.Text);
            para = cmd.Parameters.AddWithValue("@currency", lstCurrencies.SelectedValue);
            DataSet ds = new DataSet();
            ret = TinyDAL.Exec(ref cmd, ref ds);
            if (ret == 0)
            {
                if (ds.Tables[0].Rows.Count == 0)
                    lblError.Text = "No data!";
                GridView1.DataSource = ds;
                GridView1.DataBind();
                return;
            }
            if (ret == 1)
            {
                lblError.Text = "Please search the date within 3 days!";
                return;
            }
            else
            {
                lblError.Text = "Unknown error: " + ret.ToString();
                return;
            }
        }
    }
}